<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>Test</title>
<script language="JavaScript" src="../jsunit/app/jsUnitCore.js"></script>
<script language="JavaScript" src="../simple-template.js"></script>
<script type="text/javascript">
var t;
function $(id){
	return document.getElementById(id);
}
function assertError(expected, fn){
	try{
		fn();
	}catch(e){
		assertEquals(expected, e);
		return;
	}
	fail("no error");
}
function assertErrorMessage(expected, fn){
	try{
		fn();
	}catch(e){
		assertEquals(expected, e.message);
		return;
	}
	fail("no error");
}
function setUp(){
	t = new SimpleTemplate();
}
function tearDown(){
}
function testFirst(){
	assertEquals("test15TEST", t.parse("first"));
}
function testIdNotFound(){
	assertError("element by id[ttttt] not found", function(){ t.parse("ttttt"); });
}
function testIdNotFound2(){
	assertError("element by id[undefined] not found", function(){ t.parse(undefined); });
}
function testIdNotFound3(){
	assertError("element by id[null] not found", function(){ t.parse(null); });
}
function testMultiLine(){
	assertEquals("test10TEST", t.parse("multi-line"));
}
function testParseError(){
	assertEquals("element by id[ttt] not found", t.parse("ttt",null,{parseError:"output"}));
}
function testParseError2(){
	assertEquals("element by id[tttx] not found", new SimpleTemplate({parseError:"output"}).parse("tttx",null));
}
function testParseError3(){
	assertEquals("element by id[txtt] not found", new SimpleTemplate({parseError:"throw"}).parse("txtt",null,{parseError:"output"}));
}
function testParseError4(){
	assertError("element by id[txtttt] not found", function(){ new SimpleTemplate({parseError:"output"}).parse("txtttt",null,{parseError:"throw"}); });
}
function testAddon(){
	assertEquals("test_25", t.parse("addon",{value:5},{addon:{test:function(v1,v2,v3,v4){return v1+v2+v3+v4}}}));
}
function testLeftRight(){
	assertEquals("24_1", t.parse("left-right",{value:8},{left:"#",right:"##"}));
	assertEquals("24_1", new SimpleTemplate({left:"#",right:"##"}).parse("left-right",{value:8}));
	assertEquals("24_1", new SimpleTemplate({left:"#",right:"#$"}).parse("left-right",{value:8},{right:"##"}));
}
function testSeparator(){
	assertEquals("$#", t.parse("sepa",{value:8},{separator:"!!"}));
	assertEquals("$#", new SimpleTemplate({separator:"!!"}).parse("sepa"));
}
function testInput(){
	assertEquals("test15TEST", t.parse(document.getElementById("first"),null,{input:"element"}));
	assertEquals("test15TEST", new SimpleTemplate({input:"element"}).parse(document.getElementById("first")));
	assertEquals("test15TEST", new SimpleTemplate({input:"html"}).parse(document.getElementById("first"),null,{input:"element"}));
	assertEquals("test8TEST", t.parse("test$# 3 + 5 #TEST",null,{input:"html"}));
	assertEquals("test8TEST", new SimpleTemplate({input:"html"}).parse("test$# 3 + 5 #TEST"));
	assertEquals("test8TEST", new SimpleTemplate({input:"element"}).parse("test$# 3 + a #TEST",{a:5},{input:"html"}));
}
function testOutput(){
	assertEquals("test27TEST", t.parse("output",null,{output:"element"}).innerHTML);
	assertEquals("test27TEST", new SimpleTemplate({output:"element"}).parse("output").innerHTML);
	assertEquals("test27TEST", new SimpleTemplate({output:"html"}).parse("output",null,{output:"element"}).innerHTML);
}
function testEvalError(){
	try{
		t.parse("test$# 3 + 5x $ #TEST",null,{input:"html",evalError:"throw"});
	}catch(e){
		info(e);
		return;
	}
	fail("no error");
}
function testEvalError2(){
	try{
		new SimpleTemplate({input:"html",evalError:"throw"}).parse("test$# 3 + 5x $#TEST");
	}catch(e){
		info(e);
		return;
	}
	fail("no error");
}
function testEvalError3(){
	try{
		new SimpleTemplate({input:"element",evalError:"output"}).parse("test$# 3 + a x #TEST",null,{input:"html",evalError:"throw"});
	}catch(e){
		info(e);
		return;
	}
	fail("no error");
}
function testEvalError4(){
	t.parse("test$# 3 + 5x $ #TEST",null,{input:"html"});
}
function testEvalError5(){
	new SimpleTemplate({input:"element",evalError:"throw"}).parse("test$# 3 + a x #TEST",null,{input:"html",evalError:"output"});
}
function testDocument(){
	var doc = document.getElementById("iframe1").contentWindow.document;
	assertEquals("test165TEST!", t.parse("first",null,{document:doc}));
	assertEquals("test165TEST!", new SimpleTemplate({document:doc}).parse("first"));
	assertEquals("test165TEST!", new SimpleTemplate({document:document}).parse("first",null,{document:doc}));
}
function testAddonChain(){
	assertEquals("27", t.parse("addonC"));
	assertEquals("67", t.parse("addonC",null,{addon:{"default":function(target, v1, v2){return target+v1*v2;}}}));
	assertEquals("67", new SimpleTemplate({addon:{"default":function(target, v1, v2){return target+v1*v2;}}}).parse("addonC"));
	assertEquals("143", new SimpleTemplate({addon:{"default":function(target, v1, v2){return target+v1*v2;}}}).parse("addonC",null,{addon:{"default":function(target, v1, v2){return target*v1+v2;}}}));
}
function testAddon(){
	assertEquals("135undefinednull9a&gt;<>aaundefinednull111<abc", t.parse("addonM"));
}
</script>
</head>
<body>
<div id="first">test$# 3 * 5 #TEST</div>
<div id="multi-line" style="display:none">test$#
	var val = 0;
	for(var i = 0; i &lt; 5; i++){
		val += i;
	}
	val;#TEST</div>
<div id="addon">$# ""
  	@default:	 "test"	 #_$#  7 @	
 test	 :4		,
 	 value, 9  #</div>
<div id="left-right"># 3 * 8 ##_#  9-value  ##</div>
<div id="sepa">$$# 3 * 4!!empty ##</div>
<iframe id="iframe1" src="iframe.html" style="display:none"></iframe>
<div id="output"><div>test$# 3 * 9 #TEST</div></div>
<div id="addonC">$# 3 * 9@default:5
,8 #</div>
<div id="addonM">$#1@default:2#$#""@default:3#$#""@defaultIfN:4#$#null@defaultIfNull:5@forceString#$#undefined@defaultIfN:6@forceString#$#
""@defaultIfU:7@forceString#$#null@defaultIfU:8@forceString#$#undefined@defaultIfU:9@forceString#$#10@empty#$#"a"@emptyIfNU#$#null@emptyIfNU@forceString#$#undefined@emptyIfNU@forceString#$#
">"@escape#$#"&lt;&gt;"@unescape#$#" aa  "@trim#$# x of a@ignoreError #$# undefined@forceString #$#
null@forceString #$# 111@forceString #$# ""@default:" &lt;abc  "@trim@unescape #</div>

<div id="escape">&&amp; &lt;"$# 3 &lt; 5 #"</div>
</body>
</html>